/************************友情链接js*********************************/
define(function(require) {
	
	var $ = require("jquery");
	var utils = require("utils");
	var ctx = utils.constants.ctx;	
	var Confirmbox = require("confirmbox");
	var Validator = require("validator");
	var friendLinkService=require("bean/friendLinkService");
	var ui = require("ui");
	var BlackBox = require("blackbox");
	var blackBox = new BlackBox();
	
	//添加友情链接
	var validator
	$.post(ctx+"/back/website/friendLinkUpdate",function(data){
		
		var confirmBox = new Confirmbox({
			 trigger: '#addFriendLinkBtn',
		        title: '添加网站',
		        message: data,
		        onConfirm: function() {
		        	friend_submit("add");
		        }
		    });
		
		confirmBox.after("show",function(){
			validator = new Validator({
	            element: $("#friend_form")
	        }).addItem({
	            element: '[name=friendTitle]',
	            required: true
	        }).addItem({
	            element: '[name=friendUrl]',
	            required: true,
	            rule: 'url'
	        });
			$("#friendUrl").blur(function(){
				var val = $("#friendUrl").val();
				if(val.indexOf("http://")==-1 && val.indexOf("https://")==-1){
					blackBox.alert("请输入以http或https开头的网址！");
				}
			});
		});
		confirmBox.after("hide",function(){
			$("#friendTitle").val("");
			$("#friendUrl").val("");
		});
			
	});
	
	//修改友情链接
	var updateCb ;
	$(".updateFriendLink").bind("click",function(){
		var friendId=$(this).attr("data").replace("friendLink_","");
		$.post(ctx+"/back/website/friendLinkUpdate",{"friendId" : friendId},function(data){
			if( typeof(updateCb) != "undefined" || !updateCb || updateCb!=0){
				updateCb = new Confirmbox({
			        title: '添加网站',
			        effect: "fade",
			        message: data,
			        onConfirm: function() {
			        	friend_submit("update");
			        }
			    });
			}else{
				updateCb.before("show",function(){
					this.set("message",data);
				});
			}
			updateCb.after("show",function(){
				validator = new Validator({
		            element: $("#friend_form")
		        }).addItem({
		            element: '[name=friendTitle]',
		            required: true
		        }).addItem({
		            element: '[name=friendUrl]',
		            required: true,
		            rule: 'url'
		        });
				$("#friendUrl").blur(function(){
					var link = $("#friendUrl").val();
					if(link.indexOf("http://")==-1 && link.indexOf("https://")==-1 && link.indexOf("ftp://")==-1 && link.indexOf("rtsp://")==-1 && link.indexOf("mms://")==-1){
						blackBox.alert("请输入以http或https或ftp或rtsp或mms开头的网址");
					}
				});
			});
		});
		if(updateCb && typeof(updateCb) !="undefined" && updateCb!=0){
			
			updateCb.show();
		}
		
	});
	
	var friend_submit=function(type){
		validator.execute(function(error) {
			if(!error){
				var friendId=$("#friendId").val();
				var title = $("#friendTitle").val();
				var friendUrl = $("#friendUrl").val();
				
				var friendLink = {
						id : friendId,
						title : title,
						linkUrl : friendUrl
				};
				try {
					if (type == "add"){
						friendLinkService.addFriendLink(friendLink);
						blackBox.alert("添加成功");
					} else {
						friendLinkService.updateFriendLink(friendLink);
						blackBox.alert("修改成功");
					}
					location.href = ctx+"/back/website/friendLinkManager";
				}catch (e){
					blackBox.alert("添加失败");
				}
			}
		});
	}
	
	//删除友情链接
	$("#deleteaddFriendLinkBtn").bind("click",function(){
		if($("input[name=items]:checkbox:checked").length<=0){
			blackBox.alert("请选择一个网站");
			return;
		}
		blackBox.confirm("确定要删除这些网站？",function(isOk){
			if(isOk){
				var array=[];
				$("input[name=items]:checkbox:checked").each(function(){
					id=$(this).attr("id").replace("friendLink_","");
					array.push(id);
				})
				
				if (array.length != 0) {
					try{
						ui.mask().show("请稍候...");
						friendLinkService.delFriendLink(array);
						ui.mask().hide();
						blackBox.alert("删除成功");
						window.location.reload();
					} catch (e) {
						ui.mask().hide();
						blackBox.alert("删除失败:"+e.message);
					}
				} 
			}
		},{
			title:'提示',
			value:'确认'
		})
	});
	
	//查询
	$("#friend_searchBtn").click(function(){
		var name=$("#friend_search_keyword").val();
		if(name==null){
			blackBox.alert("请输入网站名称");
			return;
		}
		location.href=ctx+"/back/website/friendLinkManager?name="+name;
	});
	
	//上移
	$(".moveTopFriendLink").bind("click",function(){
		var friendId=$(this).attr("dataId").replace("friendLink_","");
		var friendOrderId=$(this).attr("data").replace("friendLink_","");
		friendLinkService.friendLinkMoveUpAndDown(friendId,friendOrderId,"up");
		window.location.reload();
	});
	
	//下移
	$(".moveDownFriendLink").bind("click",function(){
		var friendId=$(this).attr("dataId").replace("friendLink_","");
		var friendOrderId=$(this).attr("data").replace("friendLink_","");
		friendLinkService.friendLinkMoveUpAndDown(friendId,friendOrderId,"down");
		window.location.reload();
	});
		
});

